package cn.dianchiyun;

import cn.dianchiyun.protocol.netty.dtu.NettyServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ApplicationContext;

import javax.annotation.PreDestroy;

/**
 * 启动器
 * @author Weir
 * @date Oct 29, 2018
 */
//@EnableTransactionManagement
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class DTUApplication implements CommandLineRunner {
	static Logger logger = LoggerFactory.getLogger(DTUApplication.class);

	public static void main(String[] args) {
		ApplicationContext context = SpringApplication.run(DTUApplication.class, args);
	}

	@Value("${netty.server.port}")
	private int port;

	@Value("${netty.server.ip}")
	private String ip;

	//@Autowired
	//private NettyStartServer socketServer;

	@Override
	public void run(String... strings) {
        /*InetSocketAddress address = new InetSocketAddress(ip, port);
        ChannelFuture future = socketServer.run(address);
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                socketServer.destroy();
            }
        });
        future.channel().closeFuture().syncUninterruptibly();*/
		NettyServer nettyServer = new NettyServer();
		try {
			nettyServer.bing(this.port);
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	@PreDestroy
	public void destroy() {
		//socketServer.destroy();
		System.out.println("我被销毁了、、、、、我是用的@PreDestory的方式、、、、、、");
	}



}
